﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public abstract class MoveUnit : Unit
{
    public int id;
    
    public GameObject obj;
    public SoldierStateManager state;// 士兵状态转换

    public UnityEngine.AI.NavMeshAgent m_NavAgent;

    public GameObject target;
    public Unit enemy;
    public int attackRange;
    public int attackDamage;
    public float lastAttackTime;//上一次攻击的时间
    public float attackInterval;//攻击的时间间隔

    public MoveUnit():base()
    {

    }
    
    public override void BeAttacked(int att)
    {
        if (currentHP <= 0)
        {
            isDisplayBlood = false;
            return;
        }

        // 不死且受伤情况血条显示
        if(currentHP < 100)
        {
            isDisplayBlood = true;
        }


        //if (!GameObject.Find("") .chargeServer)
        //    return;

        currentHP -= att;
        if(currentHP <= 0)
        {
            currentHP = 0;
            DestroyObject(gameObject);
        }
    }

    public void SetId(int id)
    {
        this.id = id;
    }

    public void FindTarget()
    {
        if (this.tag == "Player1_Soldier")
        {
            if (target == null)
            {
                target = GameObject.FindWithTag("Player 2 castle");
            }

            LayerMask mask = 1 << 12;
            Collider[] cols = Physics.OverlapSphere(this.transform.position, attackRange, mask.value);
            if (cols.Length > 0 && target.transform.tag == "Player 2 castle")
            {
                Collider currentCollider = cols[UnityEngine.Random.Range(0, cols.Length)];
                target = currentCollider.gameObject;
            }
        }
        else if (this.tag == "Player2_Soldier")
        {
            if (target == null)
            {
                target = GameObject.FindWithTag("Player 1 castle");
            }

            LayerMask mask = 1 << 11;
            Collider[] cols = Physics.OverlapSphere(this.transform.position, attackRange, mask.value);
            if (cols.Length > 0 && target.transform.tag == "Player 1 castle")
            {
                Collider currentCollider = cols[UnityEngine.Random.Range(0, cols.Length)];
                target = currentCollider.gameObject;
            }
        }
        

        if (target != null)
        {
            m_NavAgent.SetDestination(target.transform.position);
            this.transform.LookAt(target.transform);
        }
    }

    public abstract void DoAttack();
}
